package 基础语法.常用API;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class 爬虫与正则表达式 {

    public static void main(String[] args) throws Exception {
        // 身份证大全
        // https://new.guguyu.com/sfz/
        URL url = new URL("https://new.guguyu.com/sfz/");
        URLConnection connection = url.openConnection();
        BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = bf.readLine()) != null) {
            sb.append(line);
        }
         System.out.println(sb);

        Pattern pattern = Pattern.compile("<li>.+\\d{17}[\\d(?i)x]</li>");
        Matcher matcher = pattern.matcher(sb);
        int i = 0;
        while (matcher.find()) {
            System.out.println(matcher.group().replace("<li>", "").replaceAll("</li>", "") + "\n");
            System.out.println(++i);
        }

    }
}
